package cn.edu.ctbu.dp01.api;

import cn.edu.ctbu.dp01.constant.REnum;
import cn.edu.ctbu.dp01.entity.Teacher;
import cn.edu.ctbu.dp01.exception.RException;
import cn.edu.ctbu.dp01.service.TeacherService;
import cn.edu.ctbu.dp01.util.RUtil;
import cn.edu.ctbu.dp01.vo.QueryObj;
import cn.edu.ctbu.dp01.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/api/teacher")
public class TeacherApiController {
    @Autowired
    private TeacherService teacherService;

    @GetMapping("/list")


    public R<List<Teacher>> findAll() {
        List<Teacher> teachers = teacherService.getAll();
        return RUtil.success(teachers);

    }

    @GetMapping("/{id}")


    public R<Teacher> findById(@PathVariable int id) {
        Teacher teacher = teacherService.findById(id);
        return RUtil.success(teacher);

    }


    @PostMapping("/add")
    public R<Teacher> add(Teacher teacher) {
        return RUtil.success(teacherService.add(teacher));


    }

    @PutMapping("/update")
    public R<Teacher> update(Teacher teacher) {
        return RUtil.success(teacherService.update(teacher));


    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable Integer id) {
        teacherService.delete(id);
        return RUtil.success();


    }

    @GetMapping("/validateUser")
    public R validateSnoAndPassword(String tno, String password) throws Exception {
        return RUtil.success(teacherService.validateUsernameAndPasssword(tno, password));
    }

    //
    @PostMapping("/getbypage")
    public R<Page<Teacher>> getByPage(@RequestBody QueryObj<Teacher> qObj) {

        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Integer pageIndex = 0;
        Integer pageSize = 10;
        if (qObj == null) {

            Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
            Page<Teacher> teachers = teacherService.getAll(pageable);
            return RUtil.success(teachers.getContent(),teachers.getTotalElements());
        } else {
            pageIndex = qObj.getPage();
            pageSize = qObj.getLimit();
            if (qObj.getData() instanceof Teacher) {
                Teacher teacher = (Teacher) qObj.getData();
                Pageable pageable = PageRequest.of(pageIndex, pageSize, sort);
                ExampleMatcher matcher = ExampleMatcher.matching()
                        .withMatcher("name", ExampleMatcher.GenericPropertyMatchers.contains())
                        .withIgnoreNullValues();
                Example<Teacher> example = Example.of(teacher, matcher);
                Page<Teacher> teacherPage = teacherService.getAll(example, pageable);
                return RUtil.success(teacherPage.getContent(),teacherPage.getTotalElements());
            }else{
               throw new RException(REnum.QUERY_ERR);

            }
        }
    }
}